package com.ciwong.epaper.modules.epaper.adapter;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.bookstore.bean.PublishWorkContent;
import com.ciwong.epaper.modules.epaper.bean.CatalogueInfo;
import com.ciwong.epaper.modules.epaper.bean.FollowreadTextParagraphBean;
import com.ciwong.epaper.modules.epaper.bean.ListenSpeakExaminBean;
import com.ciwong.epaper.modules.epaper.bean.Module;
import com.ciwong.epaper.modules.epaper.bean.ModuleContent;
import com.ciwong.epaper.modules.epaper.bean.PublishResource;
import com.ciwong.epaper.modules.epaper.ui.TeaWorkContentActivity;
import com.ciwong.epaper.modules.epaper.util.ClickUtil;
import com.ciwong.epaper.modules.epaper.util.EpaperJumpManager;
import com.ciwong.epaper.modules.me.bean.FavoritesBook;
import com.ciwong.epaper.modules.me.bean.FollowReadWordDetailBeans;
import com.ciwong.epaper.util.RequestCode;
import com.ciwong.libs.utils.CWLog;
import com.ciwong.mobilelib.i.ViewOnClickListener;
import com.ciwong.mobilelib.utils.ToastUtil;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @description: 作业内容适配器
 * @author: wyk
 * @time: 2021/5/27 11:58
 */
public class TeaWorkContentExListAdapter extends BaseExpandableListAdapter {

    private ArrayList<Module> modules = new ArrayList<>();
    private Context mContext;
    private int WORK_CONTENT = 2;
    private Boolean isShow;


    //作业集合
    private List<PublishWorkContent> publishWorkContentList = new ArrayList<>();
    //当前选中的作业内容
    private PublishWorkContent publishWorkContent;

    //试题使用次数和得分率
    private List<PublishResource> publishResourceList;

    private CatalogueInfo catalogueInfo;
    //书籍内容
    private FavoritesBook favoritesBook;
    //是否上锁
    private Boolean islock;
    private TeaWorkContentActivity teaWorkContentActivity;

    public TeaWorkContentExListAdapter(TeaWorkContentActivity teaWorkContentActivity, ArrayList<Module> modules, FavoritesBook favoritesBook, Boolean islock, Boolean isShow, CatalogueInfo catalogueInfo, Context mContext) {
        this.teaWorkContentActivity = teaWorkContentActivity;
        this.modules = modules;
        this.favoritesBook = favoritesBook;
        this.mContext = mContext;
        this.islock = islock;
        this.isShow = isShow;
        this.catalogueInfo = catalogueInfo;

    }

    public void updateData(ArrayList<Module> modules, List<PublishWorkContent> publishWorkContents, boolean islock) {
        this.modules = modules;
        this.islock = islock;
        this.publishWorkContentList = publishWorkContents;
        notifyDataSetChanged();
    }

    public void updatepublishResourceData(List<PublishResource> publishResourceList) {
        this.publishResourceList = publishResourceList;
        notifyDataSetChanged();
    }

    @Override
    public int getGroupCount() {
        return modules.size();
    }

    @Override
    public int getChildrenCount(int i) {
        return modules.get(i).getResourceList().size();
    }

    @Override
    public Object getGroup(int i) {
        return modules.get(i);
    }

    @Override
    public Object getChild(int i, int i1) {
        return modules.get(i).getResourceList().get(i1);
    }

    @Override
    public long getGroupId(int i) {
        return i;
    }

    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
        GroupViewHolder groupViewHolder;
        if (null == view) {
            view = LayoutInflater.from(mContext).inflate(R.layout.activity_choose_work_parent, viewGroup, false);
            groupViewHolder = new GroupViewHolder();
            groupViewHolder.choose_subject_text = view.findViewById(R.id.choose_subject_text);
            view.setTag(groupViewHolder);
        } else {
            groupViewHolder = (GroupViewHolder) view.getTag();
        }
        if (favoritesBook.getPackageType() == 2) {
            groupViewHolder.choose_subject_text.setText("趣配音");
        } else {
            groupViewHolder.choose_subject_text.setText(modules.get(i).getModuleInfo().getModuleName());
        }
        return view;
    }

    @Override
    public View getChildView(final int i, final int i1, boolean b, View view, ViewGroup viewGroup) {
        final ChildViewHolder childViewHolder;
        if (null == view) {
            view = LayoutInflater.from(mContext).inflate(R.layout.activity_choose_work_child, viewGroup, false);
            childViewHolder = new ChildViewHolder();
            childViewHolder.choose_edit_img = view.findViewById(R.id.choose_edit_img);
            childViewHolder.choose_work_name = view.findViewById(R.id.choose_work_name);
            childViewHolder.choose_work_select = view.findViewById(R.id.choose_work_select);
            childViewHolder.edit_work_tips = view.findViewById(R.id.edit_work_tips);
            childViewHolder.has_publish_img = view.findViewById(R.id.has_publish_img);
            childViewHolder.lock_icon_img = view.findViewById(R.id.lock_icon_img);
            childViewHolder.choose_detail_text = view.findViewById(R.id.choose_detail_text);
            childViewHolder.choose_relong_text = view.findViewById(R.id.choose_relong_text);
            childViewHolder.choose_read_time_text = view.findViewById(R.id.choose_read_time_text);
            childViewHolder.choose_work_lay = view.findViewById(R.id.choose_work_lay);
            childViewHolder.choose_edit_rl = view.findViewById(R.id.choose_edit_rl);
            childViewHolder.choose_subject_lay = view.findViewById(R.id.choose_subject_lay);
            childViewHolder.scoring_rate_text = view.findViewById(R.id.scoring_rate_text);
            childViewHolder.total_num_text = view.findViewById(R.id.total_num_text);
            view.setTag(childViewHolder);
        } else {
            childViewHolder = (ChildViewHolder) view.getTag();
        }
        final Module itemModule = modules.get(i);
        final List<ModuleContent> itemResourceList = itemModule.getResourceList();
        final ModuleContent itemModuleContent = itemResourceList.get(i1);
        //设置是否布置过
        if (itemModuleContent.getHasDo()) {
            childViewHolder.has_publish_img.setVisibility(View.VISIBLE);
        } else {
            childViewHolder.has_publish_img.setVisibility(View.GONE);
        }
        if (islock) {
            childViewHolder.lock_icon_img.setVisibility(View.VISIBLE);
            childViewHolder.choose_work_select.setVisibility(View.GONE);
        } else {
            childViewHolder.lock_icon_img.setVisibility(View.GONE);
            childViewHolder.choose_work_select.setVisibility(View.VISIBLE);
        }
        childViewHolder.choose_work_name.setText(itemModuleContent.getResourceName());
        //        //找出当前item的发布作业内容
        if (null != publishWorkContentList) {
            for (int i2 = 0; i2 < publishWorkContentList.size(); i2++) {
                if (null != publishWorkContentList.get(i2).getResourceUrl() && !"".equals(publishWorkContentList.get(i2).getResourceUrl())
                        && null != itemModuleContent.getResourceUrl() && !"".equals(itemModuleContent.getResourceUrl())) {
                    if (itemModuleContent.getResourceUrl().equals(publishWorkContentList.get(i2).getResourceUrl())) {
                        publishWorkContent = publishWorkContentList.get(i2);
                        if (null == publishWorkContent.getScoringRate() && null != itemModuleContent.getPublishWorkContent()
                                && null != itemModuleContent.getPublishWorkContent().getScoringRate()) {
                            String scoringRate = itemModuleContent.getPublishWorkContent().getScoringRate();
                            int totalNum = itemModuleContent.getPublishWorkContent().getTotalNum();
                            publishWorkContent.setScoringRate(scoringRate);
                            publishWorkContent.setTotalNum(totalNum);
                        }
                    }
                } else if (!"".equals(publishWorkContentList.get(i2).getResourceName()) && null != publishWorkContentList.get(i2).getResourceName()) {
                    if (itemModuleContent.getResourceName().equals(publishWorkContentList.get(i2).getResourceName())) {
                        publishWorkContent = publishWorkContentList.get(i2);
                        if (null == publishWorkContent.getScoringRate() && null != itemModuleContent.getPublishWorkContent() && null != itemModuleContent.getPublishWorkContent().getScoringRate()) {
                            String scoringRate = itemModuleContent.getPublishWorkContent().getScoringRate();
                            int totalNum = itemModuleContent.getPublishWorkContent().getTotalNum();
                            publishWorkContent.setScoringRate(scoringRate);
                            publishWorkContent.setTotalNum(totalNum);
                        }
                    }
                }
            }
        }
        if (!islock) {
            childViewHolder.choose_edit_rl.setOnClickListener(new ViewOnClickListener(1000) {
                @Override
                public void avertRepeatOnClick(View view) {
                    if (ClickUtil.isFastDoubleClick()) {
                        return;
                    }
                    if (!childViewHolder.choose_work_select.isChecked()) {
                        if (publishWorkContentList.size() >= 20) {
                            ToastUtil.INSTANCE.toastCenterError("您最多可以勾选20条内容！");
                            return;
                        }
                    }
                    PublishWorkContent publishWorkContentChecked = new PublishWorkContent();
                    //单词跟读内容
                    FollowReadWordDetailBeans followReadWordDetailBeans = new FollowReadWordDetailBeans();
                    //听书模考内容
                    ListenSpeakExaminBean listenSpeakExaminBean = new ListenSpeakExaminBean();
                    //句子跟读内容
                    FollowreadTextParagraphBean followreadTextParagraphBean = new FollowreadTextParagraphBean();
                    //获取对应保存本地的题目内容
                    if (null != publishWorkContentList) {
                        for (int i2 = 0; i2 < publishWorkContentList.size(); i2++) {
                            if (null != publishWorkContentList.get(i2).getResourceUrl() && !"".equals(publishWorkContentList.get(i2).getResourceUrl())
                                    && null != itemModuleContent.getResourceUrl() && !"".equals(itemModuleContent.getResourceUrl())) {
                                if (itemModuleContent.getResourceUrl().equals(publishWorkContentList.get(i2).getResourceUrl())) {
                                    publishWorkContentChecked = publishWorkContentList.get(i2);
                                    followReadWordDetailBeans = publishWorkContentChecked.getFollowReadWordDetailBeans();
                                    listenSpeakExaminBean = publishWorkContentChecked.getListenSpeakExaminBean();
                                    followreadTextParagraphBean = publishWorkContentChecked.getFollowreadTextParagraphBean();
                                }
                            } else if (!"".equals(publishWorkContentList.get(i2).getResourceName()) && null != publishWorkContentList.get(i2).getResourceName()) {
                                if (itemModuleContent.getResourceName().equals(publishWorkContentList.get(i2).getResourceName())) {
                                    publishWorkContentChecked = publishWorkContentList.get(i2);
                                    followReadWordDetailBeans = publishWorkContentChecked.getFollowReadWordDetailBeans();
                                    listenSpeakExaminBean = publishWorkContentChecked.getListenSpeakExaminBean();
                                    followreadTextParagraphBean = publishWorkContentChecked.getFollowreadTextParagraphBean();
                                }
                            }
                        }
                    }
                    if (ModuleContent.ResourceType.RESOURCE_TYPE_WORD.equals(itemModuleContent.getResourceType())) {
                        //单词跟读
                        EpaperJumpManager.jumpToChooseWord((Activity) mContext, itemModuleContent.getResourceName(), itemModuleContent.getResourceUrl(),
                                itemModuleContent.getVersionId(), childViewHolder.choose_work_select.isChecked(), followReadWordDetailBeans,
                                publishWorkContentChecked.getTotalWorkLong(), publishWorkContentChecked.getRequirementContent(),
                                itemModuleContent.getParentVersionId(), modules.get(i).getModuleInfo().getModuleId(), itemModuleContent.getResourceType(),
                                RequestCode.TeaWorkContent.REQ_CODE_TO_CHOOSE_WORD);
                    } else if (ModuleContent.ResourceType.RESOURCE_TYPE_DICTATION.equals(itemModuleContent.getResourceType())) {
                        //报听写
                        EpaperJumpManager.jumpToChooseWord((Activity) mContext, itemModuleContent.getResourceName(),
                                itemModuleContent.getResourceUrl(), itemModuleContent.getVersionId(),
                                childViewHolder.choose_work_select.isChecked(), followReadWordDetailBeans, publishWorkContentChecked.getTotalWorkLong(),
                                publishWorkContentChecked.getRequirementContent(), itemModuleContent.getParentVersionId(), modules.get(i).getModuleInfo().getModuleId(), itemModuleContent.getResourceType(),
                                RequestCode.TeaWorkContent.REQ_CODE_TO_DICTATION);
                    } else if (ModuleContent.ResourceType.RESOURCE_TYPE_LESSON.equals(itemModuleContent.getResourceType())) {
                        //句子跟读
                        ((TeaWorkContentActivity) mContext).getDubVideoDetailFromNet(itemModuleContent.getParentVersionId(), itemModuleContent.getVersionId(),
                                modules.get(i).getModuleInfo().getModuleId(), itemModuleContent.getResourceName(), childViewHolder.choose_work_select.isChecked(), publishWorkContentChecked,
                                itemModuleContent.getResourceUrl(), itemModuleContent.getResourceType(),
                                followreadTextParagraphBean, listenSpeakExaminBean);
                    } else if (ModuleContent.ResourceType.RESOURCE_TYPE_LISTEN_SPEAK.equals(itemModuleContent.getResourceType())) {
                        //听说模考
                        ((TeaWorkContentActivity) mContext).getDubVideoDetailFromNet(itemModuleContent.getParentVersionId(), itemModuleContent.getVersionId(),
                                modules.get(i).getModuleInfo().getModuleId(), itemModuleContent.getResourceName(),
                                childViewHolder.choose_work_select.isChecked(), publishWorkContentChecked, itemModuleContent.getResourceUrl(),
                                itemModuleContent.getResourceType(), followreadTextParagraphBean, listenSpeakExaminBean);
                    } else if (ModuleContent.ResourceType.RESOURCE_TYPE_ONLINE_ANSWER.equals(itemModuleContent.getResourceType())) {
                        //在线作答
                        ((TeaWorkContentActivity) mContext).getDubVideoDetailFromNet(itemModuleContent.getParentVersionId(), itemModuleContent.getVersionId(),
                                modules.get(i).getModuleInfo().getModuleId(), itemModuleContent.getResourceName(),
                                childViewHolder.choose_work_select.isChecked(), publishWorkContentChecked, itemModuleContent.getResourceUrl(),
                                itemModuleContent.getResourceType(), followreadTextParagraphBean, listenSpeakExaminBean);
                    } else {
                        EpaperJumpManager.jumpToInteractiveTutorial(R.id.go_back, itemModuleContent.getVersionId(), itemModuleContent.getResourceName(), modules.get(i).getModuleInfo().getPackageId(), modules.get(i).getModuleInfo().getcId(), (Activity) mContext);
                    }

                }
            });
        }


        if (itemModuleContent.getIsChecked() == 1) {
            // childViewHolder.choose_detail_text.setVisibility(View.VISIBLE);
            childViewHolder.choose_work_select.setChecked(true);
            childViewHolder.choose_work_select.setSelected(true);
            if (ModuleContent.ResourceType.RESOURCE_TYPE_WORD.equals(itemModuleContent.getResourceType())) {
                //单词跟读
                setTextDetail(publishWorkContent, childViewHolder.choose_detail_text, childViewHolder.choose_relong_text, childViewHolder.choose_read_time_text,
                        childViewHolder.scoring_rate_text, childViewHolder.total_num_text, RequestCode.TeaWorkContent.REQ_CODE_TO_CHOOSE_WORD);
            } else if (ModuleContent.ResourceType.RESOURCE_TYPE_DICTATION.equals(itemModuleContent.getResourceType())) {
                //报听写
                setTextDetail(publishWorkContent, childViewHolder.choose_detail_text, childViewHolder.choose_relong_text, childViewHolder.choose_read_time_text,
                        childViewHolder.scoring_rate_text, childViewHolder.total_num_text, RequestCode.TeaWorkContent.REQ_CODE_TO_DICTATION);

            } else if (ModuleContent.ResourceType.RESOURCE_TYPE_LESSON.equals(itemModuleContent.getResourceType())) {
                //句子跟读
                setTextDetail(publishWorkContent, childViewHolder.choose_detail_text, childViewHolder.choose_relong_text, childViewHolder.choose_read_time_text,
                        childViewHolder.scoring_rate_text, childViewHolder.total_num_text, RequestCode.TeaWorkContent.REQ_CODE_TO_CHOOSE_SENTENCE);

            } else if (ModuleContent.ResourceType.RESOURCE_TYPE_LISTEN_SPEAK.equals(itemModuleContent.getResourceType())) {
                //听说模考
                setTextDetail(publishWorkContent, childViewHolder.choose_detail_text, childViewHolder.choose_relong_text, childViewHolder.choose_read_time_text,
                        childViewHolder.scoring_rate_text, childViewHolder.total_num_text, RequestCode.TeaWorkContent.REQ_CODE_TO_CHOOSE_LISTEN_SPEAK);

            } else if (ModuleContent.ResourceType.RESOURCE_TYPE_ONLINE_ANSWER.equals(itemModuleContent.getResourceType())) {
                //在线作答
                setTextDetail(publishWorkContent, childViewHolder.choose_detail_text, childViewHolder.choose_relong_text, childViewHolder.choose_read_time_text,
                        childViewHolder.scoring_rate_text, childViewHolder.total_num_text, RequestCode.TeaWorkContent.REQ_CODE_TO_CHOOSE_LISTEN_SPEAK);

            } else {
                if (null != publishWorkContent) {
                    double refLongDouble = (double) publishWorkContent.getRefLong() / 60;
                    int workRefLong = (int) (Math.ceil(refLongDouble));
                    SpannableStringBuilder chooseWorkLongText = new SpannableStringBuilder("预计" + workRefLong + "分钟");
                    ForegroundColorSpan GreenSpan = new ForegroundColorSpan(Color.parseColor("#313944"));
                    if (workRefLong < 10) {
                        chooseWorkLongText.setSpan(GreenSpan, 2, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    } else {
                        chooseWorkLongText.setSpan(GreenSpan, 2, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    }
                    childViewHolder.choose_detail_text.setText(chooseWorkLongText);
                }
            }
            //childViewHolder.choose_edit_img.setVisibility(View.VISIBLE);
            if (null == isShow || !isShow) {
                childViewHolder.edit_work_tips.setVisibility(View.VISIBLE);
            } else {
                childViewHolder.edit_work_tips.setVisibility(View.GONE);
            }
        } else if (2 == itemModuleContent.getIsChecked() || 0 == itemModuleContent.getIsChecked()) {
            childViewHolder.choose_detail_text.setVisibility(View.GONE);
            childViewHolder.choose_relong_text.setVisibility(View.GONE);
            childViewHolder.choose_read_time_text.setVisibility(View.GONE);
            setScoringRateData(itemModuleContent, childViewHolder.total_num_text, childViewHolder.scoring_rate_text);
            childViewHolder.choose_work_select.setChecked(false);
            childViewHolder.choose_work_select.setSelected(true);
        } else {
            if (null == isShow || !isShow) {
                childViewHolder.edit_work_tips.setVisibility(View.VISIBLE);
            } else {
                childViewHolder.edit_work_tips.setVisibility(View.GONE);


            }
            setScoringRateData(itemModuleContent, childViewHolder.total_num_text, childViewHolder.scoring_rate_text);
            childViewHolder.choose_work_select.setChecked(true);
            childViewHolder.choose_work_select.setSelected(false);
        }

        childViewHolder.choose_subject_lay.setOnClickListener(new ViewOnClickListener() {
            @Override
            public void avertRepeatOnClick(View view) {
                if (islock) {
                    teaWorkContentActivity.deblockingDialog(i, i1);
                } else {
                    selectChoosWorkItem(childViewHolder, i, i1);
                }
            }
        });

        return view;
    }

    private void setScoringRateData(ModuleContent itemModuleContent, TextView total_num_text, TextView scoring_rate_text) {
        int totalNum = 0;
        String scoringRate = null;
        //设置得分率和布置总次数
        if (null != publishResourceList && publishResourceList.size() > 0) {
            for (int i = 0; i < publishResourceList.size(); i++) {
                if (publishResourceList.get(i).getResourceType().equals(itemModuleContent.getResourceType()) && publishResourceList.get(i).getVersionId().equals(itemModuleContent.getVersionId())) {
                    float ScoringRate = (float) publishResourceList.get(i).getAvgScore() / publishResourceList.get(i).getWorkScore() * 100;
                    DecimalFormat df = new DecimalFormat("0.0");
                    totalNum = publishResourceList.get(i).getTotalNum();
                    scoringRate = df.format(ScoringRate);
                    break;
                }
            }
        }
        if (scoringRate != null) {
            scoring_rate_text.setVisibility(View.VISIBLE);
            total_num_text.setVisibility(View.VISIBLE);
            SpannableStringBuilder publishResourceText = new SpannableStringBuilder("得分率: " + scoringRate + "%");
            SpannableStringBuilder totalNumText = new SpannableStringBuilder("总使用次数: " + totalNum + "次");
            ForegroundColorSpan GreenSpan = new ForegroundColorSpan(Color.parseColor("#5ce191"));
            publishResourceText.setSpan(GreenSpan, 5, 5 + scoringRate.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            //计算出布置次数有多少位 设置文本染色
            int count = LengthNum(totalNum);
            totalNumText.setSpan(GreenSpan, 7, 7 + count, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            scoring_rate_text.setText(publishResourceText);
            total_num_text.setText(totalNumText);
        } else {
            scoring_rate_text.setVisibility(View.GONE);
            total_num_text.setVisibility(View.GONE);
        }
    }

    private void selectChoosWorkItem(ChildViewHolder childViewHolder, final int i, final int i1) {
        final Module itemModule = modules.get(i);
        final List<ModuleContent> itemResourceList = itemModule.getResourceList();
        final ModuleContent itemModuleContent = itemResourceList.get(i1);
        if (publishWorkContentList.size() >= 20) {
            ToastUtil.INSTANCE.toastCenterError("您最多可以勾选20条内容！");
            return;
        }
        if (!childViewHolder.choose_work_select.isChecked()) {

            if (ModuleContent.ResourceType.RESOURCE_TYPE_ESSAY.equals(itemModuleContent.getResourceType())) {
                ToastUtil.INSTANCE.toastCenterError(R.string.not_support_the_assignment_of_news);
                return;
            }

            //先将作业对象取出转为json再赋值 避免修改原有的值
            PublishWorkContent oldWorkContent = itemModuleContent.getPublishWorkContent();
            if (null != oldWorkContent) {
                JSONObject jsonContent = (JSONObject) JSONObject.toJSON(oldWorkContent);
                PublishWorkContent publishWorkContent = jsonContent.toJavaObject(PublishWorkContent.class);
                childViewHolder.choose_work_select.setChecked(true);
                childViewHolder.choose_work_select.setSelected(true);
                if (null == isShow || !isShow) {
                    childViewHolder.edit_work_tips.setVisibility(View.VISIBLE);
                } else {
                    childViewHolder.edit_work_tips.setVisibility(View.GONE);
                }
                itemModuleContent.setIsChecked(1);
                publishWorkContent.setParentVersionId(itemModuleContent.getParentVersionId());
                publishWorkContent.setVersionId(itemModuleContent.getVersionId());
                publishWorkContent.setResourceName(itemModuleContent.getResourceName());
                publishWorkContent.setResourceUrl(itemModuleContent.getResourceUrl());
                publishWorkContent.setResourceType(itemModuleContent.getResourceType());
                if (null != catalogueInfo) {
                    publishWorkContent.setWorkName(catalogueInfo.getName());
                }
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("readtimes", 1);
                jsonObject.put("speekingtype", 1);
                publishWorkContent.setRequirementContent(jsonObject.toString());
                if (favoritesBook.getPackageType() == 2) {
                    publishWorkContent.setModuleId(129);
                } else {
                    publishWorkContent.setModuleId(modules.get(i).getModuleInfo().getModuleId());
                }

                if (ModuleContent.ResourceType.RESOURCE_TYPE_WORD.equals(itemModuleContent.getResourceType())) {
                    //单词跟读
                    setTextDetail(publishWorkContent, childViewHolder.choose_detail_text, childViewHolder.choose_relong_text, childViewHolder.choose_read_time_text,
                            childViewHolder.scoring_rate_text, childViewHolder.total_num_text, RequestCode.TeaWorkContent.REQ_CODE_TO_CHOOSE_WORD);
                    publishWorkContent.setFinish(true);
                    publishWorkContentList.add(publishWorkContent);
                    if (mContext instanceof TeaWorkContentActivity) {
                        ((TeaWorkContentActivity) mContext).setWorkCountAndTimeConsuming(2, publishWorkContent.getVersionId(), publishWorkContent.getResourceUrl(), publishWorkContent.getResourceName());
                        ((TeaWorkContentActivity) mContext).setWorkLong();
                    }
                    childViewHolder.choose_edit_img.setVisibility(View.VISIBLE);
                } else if (ModuleContent.ResourceType.RESOURCE_TYPE_DICTATION.equals(itemModuleContent.getResourceType())) {
                    //报听写
                    setTextDetail(publishWorkContent, childViewHolder.choose_detail_text, childViewHolder.choose_relong_text, childViewHolder.choose_read_time_text,
                            childViewHolder.scoring_rate_text, childViewHolder.total_num_text, RequestCode.TeaWorkContent.REQ_CODE_TO_DICTATION);
                    publishWorkContent.setFinish(true);
                    publishWorkContentList.add(publishWorkContent);
                    if (mContext instanceof TeaWorkContentActivity) {
                        ((TeaWorkContentActivity) mContext).setWorkCountAndTimeConsuming(2, publishWorkContent.getVersionId(), publishWorkContent.getResourceUrl(), publishWorkContent.getResourceName());
                        ((TeaWorkContentActivity) mContext).setWorkLong();
                    }
                    childViewHolder.choose_edit_img.setVisibility(View.VISIBLE);
                } else if (ModuleContent.ResourceType.RESOURCE_TYPE_LESSON.equals(itemModuleContent.getResourceType())) {
                    //句子跟读
                    setTextDetail(publishWorkContent, childViewHolder.choose_detail_text, childViewHolder.choose_relong_text, childViewHolder.choose_read_time_text,
                            childViewHolder.scoring_rate_text, childViewHolder.total_num_text, RequestCode.TeaWorkContent.REQ_CODE_TO_CHOOSE_SENTENCE);

                    publishWorkContent.setFinish(true);
                    publishWorkContentList.add(publishWorkContent);
                    if (mContext instanceof TeaWorkContentActivity) {
                        ((TeaWorkContentActivity) mContext).setWorkCountAndTimeConsuming(2, publishWorkContent.getVersionId(), publishWorkContent.getResourceUrl(), publishWorkContent.getResourceName());
                        ((TeaWorkContentActivity) mContext).setWorkLong();
                    }
                    childViewHolder.choose_edit_img.setVisibility(View.VISIBLE);
                } else if (ModuleContent.ResourceType.RESOURCE_TYPE_LISTEN_SPEAK.equals(itemModuleContent.getResourceType())) {
                    //听说模考
                    setTextDetail(publishWorkContent, childViewHolder.choose_detail_text, childViewHolder.choose_relong_text, childViewHolder.choose_read_time_text,
                            childViewHolder.scoring_rate_text, childViewHolder.total_num_text, RequestCode.TeaWorkContent.REQ_CODE_TO_CHOOSE_LISTEN_SPEAK);
                    publishWorkContent.setFinish(true);
                    publishWorkContentList.add(publishWorkContent);
                    if (mContext instanceof TeaWorkContentActivity) {
                        ((TeaWorkContentActivity) mContext).setWorkCountAndTimeConsuming(2, publishWorkContent.getVersionId(), publishWorkContent.getResourceUrl(), publishWorkContent.getResourceName());
                        ((TeaWorkContentActivity) mContext).setWorkLong();
                    }
                    childViewHolder.choose_edit_img.setVisibility(View.VISIBLE);
                } else if (ModuleContent.ResourceType.RESOURCE_TYPE_ONLINE_ANSWER.equals(itemModuleContent.getResourceType())) {
                    //在线作答
                    setTextDetail(publishWorkContent, childViewHolder.choose_detail_text, childViewHolder.choose_relong_text, childViewHolder.choose_read_time_text,
                            childViewHolder.scoring_rate_text, childViewHolder.total_num_text, RequestCode.TeaWorkContent.REQ_CODE_TO_CHOOSE_LISTEN_SPEAK);
                    publishWorkContent.setFinish(true);
                    publishWorkContentList.add(publishWorkContent);
                    if (mContext instanceof TeaWorkContentActivity) {
                        ((TeaWorkContentActivity) mContext).setWorkCountAndTimeConsuming(2, publishWorkContent.getVersionId(), publishWorkContent.getResourceUrl(), publishWorkContent.getResourceName());
                        ((TeaWorkContentActivity) mContext).setWorkLong();
                    }
                    childViewHolder.choose_edit_img.setVisibility(View.VISIBLE);
                } else {
                    childViewHolder.choose_detail_text.setVisibility(View.VISIBLE);
                    childViewHolder.choose_relong_text.setVisibility(View.VISIBLE);
                    childViewHolder.choose_read_time_text.setVisibility(View.GONE);
                    //childViewHolder.scoring_rate_text.setVisibility(View.VISIBLE);
                    SpannableStringBuilder publishResourceText = new SpannableStringBuilder("得分率: " + publishWorkContent.getScoringRate() + "%");
                    SpannableStringBuilder totalNumText = new SpannableStringBuilder("总使用次数: " + publishWorkContent.getTotalNum() + "次");
                    if (null != publishWorkContent) {
                        if (null != publishWorkContent.getScoringRate() && !publishWorkContent.getScoringRate().equals("0.0")) {
                            childViewHolder.scoring_rate_text.setVisibility(View.VISIBLE);
                            publishResourceText = new SpannableStringBuilder("得分率: " + publishWorkContent.getScoringRate() + "%");
                        } else {
                            childViewHolder.scoring_rate_text.setVisibility(View.GONE);
                        }

                        if (publishWorkContent.getTotalNum() != 0) {
                            childViewHolder.total_num_text.setVisibility(View.VISIBLE);
                            totalNumText = new SpannableStringBuilder("总使用次数: " + publishWorkContent.getTotalNum() + "次");
                        } else {
                            childViewHolder.total_num_text.setVisibility(View.GONE);
                        }
                        int workRefLong = (int) (Math.ceil((double) publishWorkContent.getRefLong() / 60));
                        childViewHolder.choose_relong_text.setText("预计" + workRefLong + "分钟");
                        ForegroundColorSpan GreenSpan = new ForegroundColorSpan(Color.parseColor("#5ce191"));
                        if (null != publishWorkContent.getScoringRate()) {
                            publishResourceText.setSpan(GreenSpan, 5, 5 + publishWorkContent.getScoringRate().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                        } else {
                            publishResourceText.setSpan(GreenSpan, 5, 6, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                        }
                        //计算出布置次数有多少位 设置文本染色
                        int count = LengthNum(publishWorkContent.getTotalNum());
                        totalNumText.setSpan(GreenSpan, 7, 7 + count, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                        childViewHolder.scoring_rate_text.setText(publishResourceText);
                        childViewHolder.total_num_text.setText(totalNumText);
                        publishWorkContent.setFinish(true);
                        publishWorkContentList.add(publishWorkContent);
                        if (mContext instanceof TeaWorkContentActivity) {
                            ((TeaWorkContentActivity) mContext).setWorkCountAndTimeConsuming(2, publishWorkContent.getVersionId(), publishWorkContent.getResourceUrl(), publishWorkContent.getResourceName());
                            ((TeaWorkContentActivity) mContext).setWorkLong();
                        }
                        childViewHolder.choose_edit_img.setVisibility(View.VISIBLE);
                    }
                    childViewHolder.choose_work_select.setChecked(true);
                }
            }
        } else {
            childViewHolder.choose_work_select.setChecked(false);
            childViewHolder.choose_work_select.setSelected(true);
            PublishWorkContent publishWorkContent = new PublishWorkContent();
            if (null != publishWorkContentList) {
                for (int i2 = 0; i2 < publishWorkContentList.size(); i2++) {
                    if (null != publishWorkContentList.get(i2).getResourceUrl() && !"".equals(publishWorkContentList.get(i2).getResourceUrl())
                            && null != itemModuleContent.getResourceUrl() && !"".equals(itemModuleContent.getResourceUrl())) {
                        if (itemModuleContent.getResourceUrl().equals(publishWorkContentList.get(i2).getResourceUrl())) {
                            publishWorkContent = publishWorkContentList.get(i2);
                        }
                    } else if (!"".equals(publishWorkContentList.get(i2).getVersionId()) && null != publishWorkContentList.get(i2).getVersionId()) {
                        if (itemModuleContent.getVersionId().equals(publishWorkContentList.get(i2).getVersionId())) {
                            publishWorkContent = publishWorkContentList.get(i2);
                        }
                    }
                }
            }
            itemModuleContent.setIsChecked(2);
            childViewHolder.choose_work_select.setChecked(false);
            childViewHolder.choose_detail_text.setVisibility(View.GONE);
            childViewHolder.choose_read_time_text.setVisibility(View.GONE);
            childViewHolder.choose_relong_text.setVisibility(View.GONE);
            childViewHolder.total_num_text.setVisibility(View.GONE);
            childViewHolder.scoring_rate_text.setVisibility(View.GONE);
            childViewHolder.edit_work_tips.setVisibility(View.GONE);

            if (publishWorkContentList.size() > 0) {
                for (int a = 0; a < publishWorkContentList.size(); a++) {
                    if (null != publishWorkContentList.get(a).getResourceUrl() && null != itemModuleContent.getResourceUrl() && !"".equals(publishWorkContentList.get(a).getResourceUrl()) && !"".equals(itemModuleContent.getResourceUrl())) {
                        if (itemModuleContent.getResourceUrl().equals(publishWorkContentList.get(a).getResourceUrl())) {
                            publishWorkContentList.remove(a);
                            break;
                        }
                    } else {
                        if (itemModuleContent.getResourceName().equals(publishWorkContentList.get(a).getResourceName())) {
                            publishWorkContentList.remove(a);
                            break;
                        }
                    }

                }
            }
            if (mContext instanceof TeaWorkContentActivity) {
                ((TeaWorkContentActivity) mContext).setWorkCountAndTimeConsuming(2, publishWorkContent.getVersionId(), publishWorkContent.getResourceUrl(), publishWorkContent.getResourceName());
                ((TeaWorkContentActivity) mContext).setWorkLong();
            }
            if (null != publishWorkContentList && publishWorkContentList.size() > 0) {
                publishWorkContentList.get(publishWorkContentList.size() - 1).setFinish(true);
            }
        }
    }

    /**
     * 设置选题详情
     *
     * @param publishWorkContent    作业内容
     * @param choose_detail_text    作业详情text
     * @param choose_relong_text    作业时长text
     * @param choose_read_time_text 跟读次数text
     * @param scoring_rate_text     作业得分率text
     * @param tag
     */
    public void setTextDetail(PublishWorkContent publishWorkContent, TextView choose_detail_text, TextView choose_relong_text,
                              TextView choose_read_time_text, TextView scoring_rate_text, TextView total_num_text, int tag) {
        try {
            if (null != publishWorkContent) {
                choose_detail_text.setVisibility(View.VISIBLE);
                choose_relong_text.setVisibility(View.VISIBLE);
                //total_num_text.setVisibility(View.VISIBLE);
                //scoring_rate_text.setVisibility(View.VISIBLE);
                String checkedResource = publishWorkContent.getCheckedResource();
                String readtimes = "1";
                String speekingtype = "跟读";
                int checkCount = 0;
                if (null != checkedResource && !"".equals(checkedResource)) {
                    String[] split = checkedResource.split(",");
                    checkCount = split.length;
                } else {
                    checkCount = publishWorkContent.getTotalQuesNum();
                }

                String requirementContent = publishWorkContent.getRequirementContent();
                JSONObject parse = JSONObject.parseObject(requirementContent);
                if (parse.size() != 0) {
                    speekingtype = parse.getString("speekingtype");
                    if (speekingtype.equals("1")) {
                        speekingtype = "跟读";
                    } else if (speekingtype.equals("2")) {
                        speekingtype = "朗读";
                    } else {
                        speekingtype = "背诵";
                    }
                    readtimes = parse.getString("readtimes");
                }


                SpannableStringBuilder chooseCountText = new SpannableStringBuilder();
                if (tag == RequestCode.TeaWorkContent.REQ_CODE_TO_CHOOSE_WORD) {
                    choose_read_time_text.setVisibility(View.VISIBLE);
                    chooseCountText = new SpannableStringBuilder("已选 " + checkCount + "个单词");
                } else if (tag == RequestCode.TeaWorkContent.REQ_CODE_TO_DICTATION) {
                    choose_read_time_text.setVisibility(View.GONE);
                    chooseCountText = new SpannableStringBuilder("已选 " + checkCount + "个单词");
                } else if (tag == RequestCode.TeaWorkContent.REQ_CODE_TO_CHOOSE_SENTENCE) {
                    if (publishWorkContent.getModuleId() == 129) {
                        choose_read_time_text.setVisibility(View.GONE);
                    } else {
                        choose_read_time_text.setVisibility(View.VISIBLE);
                    }
                    chooseCountText = new SpannableStringBuilder("已选 " + checkCount + "个句子");
                } else if (tag == RequestCode.TeaWorkContent.REQ_CODE_TO_CHOOSE_LISTEN_SPEAK) {
                    choose_read_time_text.setVisibility(View.GONE);
                    DecimalFormat df = new DecimalFormat("0");
                    chooseCountText = new SpannableStringBuilder("已选 " + checkCount + "个大题,总分 " + df.format(publishWorkContent.getWorkScore()) + "分");
                }
                int workRefLong = (int) (Math.ceil((double) publishWorkContent.getRefLong() / 60));
                SpannableStringBuilder chooseWorkLongText = new SpannableStringBuilder("预计 " + workRefLong + " 分钟");
                SpannableStringBuilder chooseReadTimeText = new SpannableStringBuilder(speekingtype + " " + readtimes + " 次");
                SpannableStringBuilder publishResourceText = new SpannableStringBuilder("得分率: " + publishWorkContent.getScoringRate() + "%");
                SpannableStringBuilder totalNumText = new SpannableStringBuilder("总使用次数: " + publishWorkContent.getTotalNum() + "次");
                if (null != publishWorkContent.getScoringRate() && !publishWorkContent.getScoringRate().equals("0.0")) {
                    scoring_rate_text.setVisibility(View.VISIBLE);
                    publishResourceText = new SpannableStringBuilder("得分率: " + publishWorkContent.getScoringRate() + "%");
                } else {
                    scoring_rate_text.setVisibility(View.GONE);
                }
                if (publishWorkContent.getTotalNum() != 0) {
                    total_num_text.setVisibility(View.VISIBLE);
                    totalNumText = new SpannableStringBuilder("总使用次数: " + publishWorkContent.getTotalNum() + "次");
                } else {
                    total_num_text.setVisibility(View.GONE);
                }
                ForegroundColorSpan GreenSpan = new ForegroundColorSpan(Color.parseColor("#5ce191"));
                //计算出选择题目数有多少位 设置文本染色
                int totalNumCount = LengthNum(checkCount);
                chooseCountText.setSpan(GreenSpan, 3, 3 + totalNumCount, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                if (workRefLong < 10) {
                    chooseWorkLongText.setSpan(GreenSpan, 3, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                } else {
                    chooseWorkLongText.setSpan(GreenSpan, 3, 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                }
                if (null != publishWorkContent.getScoringRate()) {
                    publishResourceText.setSpan(GreenSpan, 5, 5 + publishWorkContent.getScoringRate().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                } else {
                    publishResourceText.setSpan(GreenSpan, 5, 6, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                }
                //计算出布置次数有多少位 设置文本染色
                int count = LengthNum(publishWorkContent.getTotalNum());
                totalNumText.setSpan(GreenSpan, 7, 7 + count, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                chooseReadTimeText.setSpan(GreenSpan, 3, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                choose_detail_text.setText(chooseCountText);
                choose_relong_text.setText(chooseWorkLongText);
                choose_read_time_text.setText(chooseReadTimeText);
                scoring_rate_text.setText(publishResourceText);
                total_num_text.setText(totalNumText);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 计算一个整数有多少位
     *
     * @param num
     * @return
     */
    public static int LengthNum(int num) {
        int count = 0; //计数
        try {
            while (num >= 1) {
                num /= 10;
                count++;
            }
            return count;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return count;
    }


    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }

    private class GroupViewHolder {
        private TextView choose_subject_text;
    }

    private class ChildViewHolder {
        private TextView choose_work_name;
        private ImageView choose_edit_img;
        private TextView scoring_rate_text;
        private ImageView edit_work_tips;
        private CheckBox choose_work_select;
        private ImageView has_publish_img;
        private ImageView lock_icon_img;
        private TextView choose_detail_text;
        private TextView choose_relong_text;
        private TextView choose_read_time_text;
        private TextView total_num_text;
        private RelativeLayout choose_work_lay;
        private RelativeLayout choose_subject_lay;
        private RelativeLayout choose_edit_rl;
    }
}
